home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-09-17 | 4.9 KB | 146 lines | [TEXT/MPS ] |
- //========================================================================================
- //
- // File: FWThrdGd.h
- // Release Version: $ ODF 2 $
- //
- // Copyright: (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
- //
- //========================================================================================
-
- #ifndef FWTHRDGD_H
- #define FWTHRDGD_H
-
- #ifndef FWSTDDEF_H
- #include "FWStdDef.h"
- #endif
-
- #ifdef FW_BUILD_MAC
- #include <Threads.h>
- #endif
-
- //==============================================================================
- // Theory of Operation
- //==============================================================================
-
- // FW_CThreadSafe is a class exporting static methods which parts
- // must call if they are using threads. The methods should be called
- // within a critical region; they are not thread-safe.
-
- // FW_CThreadGuard is an abstract base class associating threads with operations
- // which must be done to assure thread-safe execution. Instances of FW_CThreadGuard
- // are always statically allocated and provide protection for global variables.
-
- //==============================================================================
- // Constants
- //==============================================================================
-
- //==============================================================================
- // Scalar Types
- //==============================================================================
-
- #if defined FW_BUILD_MAC
- typedef ThreadID FW_ThreadID;
- #elif defined FW_BUILD_WIN
- typedef unsigned long FW_ThreadID;
- #endif
-
- //=====================================================================================
- // Classes defined in this interface
- //=====================================================================================
-
- class FW_CThreadSafe;
- class FW_CThreadGuard;
-
- //=====================================================================================
- // Classes used by this interface
- //=====================================================================================
-
- //=====================================================================================
- // Class FW_CThreadSafe
- //=====================================================================================
-
- class FW_CThreadSafe {
- public:
- static void NoteCreation(FW_ThreadID newlyCreatedThread);
- static void NoteTermination(FW_ThreadID threadBeingKilled);
- // Parts call NoteCreation when creating a new thread and
- // NoteTermination when a thread is being destroyed.
-
- #ifdef FW_BUILD_MAC
- static void NoteSwitch(FW_ThreadID aThread, FW_Boolean switchingIn);
- // A thread-using part must call this method when a thread
- // switch occurs. The boolean is TRUE when the thread is being
- // switched in, FALSE when the thread is being switched out.
- #endif
-
- private:
- friend class FW_CThreadGuard;
- static void RegisterTerminationGuard(FW_CThreadGuard *aThreadGuard);
- static void DeRegisterTerminationGuard(FW_CThreadGuard *aThreadGuard);
-
- static void RegisterSwitchGuard(FW_CThreadGuard *aThreadGuard);
- static void DeRegisterSwitchGuard(FW_CThreadGuard *aThreadGuard);
-
- private:
- static FW_CThreadGuard *fgSwitchGuards;
- static FW_CThreadGuard *fgTerminationGuards;
- };
-
-
- //=====================================================================================
- // Class FW_CThreadGuard
- //=====================================================================================
-
- class FW_CThreadGuard {
- protected:
- enum {
- kNoThreadEvent = 0x00,
- kSwitchInThreadEvent = 0x01,
- kSwitchOutThreadEvent = 0x02,
-
- kAllThreadEvents = kSwitchInThreadEvent | kSwitchOutThreadEvent
- };
-
- FW_CThreadGuard(unsigned long threadEventMask = kAllThreadEvents);
- virtual ~FW_CThreadGuard();
-
- virtual void Created(FW_ThreadID newlyCreatedThread) = 0;
- virtual void Terminating(FW_ThreadID threadBeingKilled) = 0;
- // These methods are called when threads are created and
- // terminated.
-
- #ifdef FW_BUILD_MAC
- virtual void Switch(FW_ThreadID aThread, FW_Boolean switchingIn);
- // Called when a thread switch takes place. This is an optional
- // notification: if the constructor did not have kSwitchThreadEvent
- // in its bitmask the guard will not be notified of thread switches.
- // switchingIn is TRUE if aThread will be next to run, FALSE if it
- // being switched out.
- #endif
-
- void AddThreadInfo(FW_ThreadID aThreadID, void *threadInfo);
- void RemoveThreadInfo(FW_ThreadID aThreadID);
- void *GetThreadInfo(FW_ThreadID aThreadID);
- // These methods allow instances of derived classes to associate
- // a chunk of data with a thread. AddThreadInfo() should be called
- // from "Created()", RemoveThreadInfo() should be called from
- // "Terminating()" and GetThreadInfo() from "Switching()".
-
- unsigned long GetMask() const;
-
- private:
- friend class FW_CThreadSafe;
- FW_CThreadGuard *fNext;
- unsigned long fMask;
-
- private:
- struct FW_SThreadItem {
- FW_ThreadID fThreadID;
- void *fThreadInfo;
- FW_SThreadItem *fNext;
- };
- FW_SThreadItem *fActiveThreads;
- };
-
- #endif
-